package com.ruoyi.novel.service.impl;

import com.ruoyi.novel.domain.*;
import com.ruoyi.novel.mapper.BookshelfMapper;
import com.ruoyi.novel.mapper.HomePageMapper;
import com.ruoyi.novel.mapper.NovelMapper;
import com.ruoyi.novel.service.BookShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookShelfServiceImpl implements BookShelfService {
    @Autowired
    private BookshelfMapper bookshelfMapper;
    @Autowired
    private HomePageMapper homePageMapper;

    /**
     * 根据用户id获取书架信息
     * @param userId
     * @return
     */
    @Override
    public List<BookshelfShow> getBookShelfByUserId(Long userId) {
        List<Bookshelf> getBookShelfByUserIds = bookshelfMapper.getBookShelfByUserId(userId);
        List<BookshelfShow> bookshelfShowList = new ArrayList<>();
        for(Bookshelf bookshelf : getBookShelfByUserIds){
            BookshelfShow bookshelfShow = new BookshelfShow();
            NovelDTO novelDTO = homePageMapper.getNovelDTO(bookshelf.getNovelId());
            bookshelfShow.setTitle(novelDTO.getTitle());
            bookshelfShow.setAuthorName(novelDTO.getAuthorName());
            bookshelfShow.setCategoryName(novelDTO.getCategoryName());
            bookshelfShow.setCoverUrl(novelDTO.getCoverUrl());
            bookshelfShow.setLastReadChapter(bookshelf.getLastReadChapter());
            bookshelfShow.setLastChapterNum(bookshelf.getLastChapterNum());
            bookshelfShow.setReadStatus(bookshelf.getReadStatus());
            bookshelfShow.setCreateTime(bookshelf.getCreateTime());
            bookshelfShow.setUpdateTime(bookshelf.getUpdateTime());
            bookshelfShowList.add(bookshelfShow);
        }
        return bookshelfShowList;
    }

    @Override
    public int addBookShelf(Long userId, String title) {
        Long novelId = bookshelfMapper.selectNovelIdByTitle(title);
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setUserId(userId);
        bookshelf.setNovelId(novelId);
        bookshelf.setReadStatus(0L);
        bookshelf.setCreateTime(new Date());
        bookshelf.setUpdateTime(new Date());
        return bookshelfMapper.insertBookshelf(bookshelf);
    }


    @Override
    public int isInBookShelf(Long userId, String title) {
        Long novelId = bookshelfMapper.selectNovelIdByTitle((title));
        return bookshelfMapper.selectBookshelf(userId,novelId);
    }

    @Override
    public int deleteBookShelf(BookDelete bookDelete) {
        Long novelId = bookshelfMapper.selectNovelIdByTitle(bookDelete.getTitle());
        return bookshelfMapper.deleteBookshelf(
                bookDelete.getUserId(), // 对应 @Param("userId")
                novelId                 // 对应 @Param("novelId")
        );
    }

    @Override
    public int deleteBatchBookShelf(BookDeleteDTO bookDeleteDTO) {
        for (String title : bookDeleteDTO.getTitles()) {
            Long novelId = bookshelfMapper.selectNovelIdByTitle( title);
            int isSuc = bookshelfMapper.deleteBookshelf(bookDeleteDTO.getUserId(), novelId);
            if(isSuc == 0){
                return 0;
            }
        }
        return 1;
    }
}
